package org.jimm.app;

import org.jimm.service.Preferences;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.GridView;
import android.widget.TextView;

/**
 * @author Aleksey Konovalov, r.u.s.h.a.n.
 */
public class SmileActivity extends Activity implements OnItemSelectedListener, OnItemClickListener {
    private TextView textTitle;
    public static final String LOG_TAG = "Jimm:SmileActivity";
    private Preferences p = JimmApplication.getInstance().getPreferences();
    //  private org.jimm.service.Preferences prefs;
    final String[] listSmile = {
            ":-)",
            ";-)",
            ":-(",
            ":-P",
            ":-[",
            ":'(",
            ":-]",
            "*hi*",
            "*kissed*",
            "*thumbs_up*",
            "*sorry*",

            "*troll*",
            "*okey*",
            "*xD*",
            "*lol*",
            "*hmm*",
    };

    //TODO read from file
    /*
     * 
     File define = new File(path+"/define.ini");
	 BufferedReader in = new BufferedReader(new FileReader(define));
	 String line = in.readLine();
	 String[] keys = line.split(",");
     */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smile);

        textTitle = (TextView) findViewById(R.id.label);

        GridView smileGrid = (GridView) findViewById(R.id.smile);

        smileGrid.setAdapter(new SmileAdapter(this));
        smileGrid.setOnItemSelectedListener(this);
        smileGrid.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        // Toast.makeText(SmileActivity.this, "" + position, Toast.LENGTH_SHORT).show();
        String smile = listSmile[position];

        textTitle.setText(smile);
        Log.i(LOG_TAG, "Selected Smile: " + smile);

        if (p.smileSpaceEnabled) {
            smile = " " + smile + " ";
        }

        Intent intent = getIntent();
        intent.putExtra("smile", smile);

        setResult(RESULT_OK, intent);

        finish();
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
        String smile = listSmile[position];
        textTitle.setText(smile);

    }

    @Override
    public void onNothingSelected(AdapterView<?> paramAdapterView) {
        textTitle.setText("");

    }
    /*
     @Override
     public void onPause() {
         super.onPause();
          JimmApplication.closeApp();
     }
     */
}